import IEntitySystem from "../../base/ECSR/IEntitySystem";
import AddBubbleComponent from "../components/AddBubbleComponent";
import MapUtil from "../utilities/MapUtil";
import BubbleComponent from "../components/BubbleComponent";
import GameClientData from "../data/GameClientData";
import MapComponent from "../components/MapComponent";
import { EntityLifeState } from "../../../scripts/base/ECSR/Entity";
import Game from "../Game";

export default class AddBubbleToMapSystem extends IEntitySystem
{
    constructor(world)
    {
        super(world);

    }
    // 每逻辑帧调用
    Execute()
    {
        let mapComponent = this.world.GetComponentByEntityId(GameClientData.mapId, "MapComponent") as MapComponent;
        let addBubbleComList = this.world.GetComponents("AddBubbleComponent") as Array<AddBubbleComponent>;

        for(let i = 0; i < addBubbleComList.length; ++i)
        {
            let addBubbleCom = addBubbleComList[i];
            let bubbleItemId = addBubbleCom.bubbleId;
            let bubbleEntity = this.world.AddEntity(bubbleItemId);
            let bubbleCom = bubbleEntity.GetComponent("BubbleComponent") as BubbleComponent;
            MapUtil.MapAddBall(bubbleItemId, bubbleCom.row, bubbleCom.col, mapComponent.bubbleMap);
            MapUtil.UpdateMapPosition(mapComponent);
            // 删除AddBubbleCom
            this.world.GetEntity(addBubbleCom.entityId).lifeState = EntityLifeState.Die;
        }
    }
}